#include <stdio.h>
#include <string.h>
#include <mysql.h>
#include <mysqld_error.h>
//#include <errmsg>
int connection(MYSQL *mysql,  const char *host, const char* user, const char* passward, const char* db)
{	
	mysql_init(&mysql);
	if(!mysql_real_connect(&mysql, host, user, passward, db, 0, NULL, 0))
       {
		return -1;

	}	
        else
                printf("connected Mysql successful!\n");
	return 1;
}
void create_database(MYSQL *mysql,  const char* query)
{
	if(mysql_query(&mysql, query))
                printf("CREATE DATABASE ERROR!\nERROR%d:%s\n",mysql_errno(&mysql),mysql_error(&mysql));
        else
                printf("CREATE DATABASE SUCCESS!\n");
}
void create_table( MYSQL *mysql, const char* query)
{
        if(mysql_query(&mysql, query))
                printf("CREATE TABLE ERROR!\nERROR%d:%s\n",mysql_errno(&mysql),mysql_error(&mysql));
        else
                printf("CREATE TABLE SUCCESS!\n");
}
int insert(MYSQL *mysql, char* query)
{	if(mysql_query(&mysql, query))
		printf("INSERT COLUMNS ERROR!\n ERROR %d:%s\n",mysql_errno(&mysql),mysql_error(&mysql));
	else
		printf("INSERT COLUMNS SUCCESS!\n");
}
/*
void PUSH_PAYLOAD(MYSQL *mysql, char *payload)
{
	connection(mysql,"localhost","root","lama940225","MQTT_DATA");
	insert(mysql, payload);
        mysql_close(&mysql);
}*/
/*
int main()
{
	int t, r;
	mysql_init(&mysql);
	connection("localhost","root","lama940225","MQTT_DATA");	
	//create a new databeses;
	create_database("CREATE DATABASE MQTT_DATA");
	create_table("CREATE TABLE DATA (  	\
		date	TIMESTAMP  ,  		\
		name	CHAR(20) ,  		\
		IP_Addr	CHAR(30),			\
		RSSI	INT,			\
		Pa_value INT,			\
		TEMP	INT, 			\
		VDD3	INT,			\
		PRIMARY KEY (date)          	\
		)ENGINE=InnoDB"   
);
	//insert("INSERT INTO DATA (name) VALUES ('liming')");
	mysql_close(&mysql);
	return 0;

}*/
